/*
 * Lenovo Group
 * Copyright (c) 2015-2016 All Rights Reserved.
 * 
 * Project Name:itsm-gateway
 * Create Time: 2016年1月30日 下午3:38:46
 */
package com.lenovo.lmrp.server.itsmgateway.repository;

import javax.inject.Inject;
import javax.inject.Named;

import com.google.inject.Binder;
import com.google.inject.Module;
import com.google.inject.Scopes;
import com.lenovo.lmrp.server.itsmgateway.domain.engineer.IEngineerRepository;
import com.lenovo.lmrp.server.itsmgateway.domain.metadata.ITicketSystemRepository;
import com.lenovo.lmrp.server.itsmgateway.domain.ticket.ITicketRepository;

/**
 * 
 * @author wujin
 * @version v1.0
 */
public abstract class ARepositoryFactory {
    private static ARepositoryFactory factory;
    
    public static ARepositoryFactory getFactory() {
        return factory;
    }
    
    @Inject
    public static void setFactory(ARepositoryFactory factory) {
        ARepositoryFactory.factory = factory;
    }
    
    public abstract ITicketSystemRepository getTicketSystemRepostory();
    
    public abstract IEngineerRepository getEngineerRepository();
    
    public abstract ITicketRepository getTicketRepository();
    
    @Named("default")
    public static class RepositoryFactoryImpl extends ARepositoryFactory {
        @Inject
        private ITicketSystemRepository ticketSystemRepository;
        
        @Inject
        private IEngineerRepository engineerRepository;
        
        @Inject
        private ITicketRepository ticketRepository;
        
        @Override
        public ITicketSystemRepository getTicketSystemRepostory() {
            return ticketSystemRepository;
        }

        @Override
        public IEngineerRepository getEngineerRepository() {
            return engineerRepository;
        }

        @Override
        public ITicketRepository getTicketRepository() {
            return ticketRepository;
        }
    }
    
    public static class RepositoryModule implements Module {

        @Override
        public void configure(Binder binder) {
            binder.bind(IEngineerRepository.class).to(EngineerRepository.class).in(Scopes.SINGLETON);
            binder.bind(ITicketRepository.class).to(TicketRepository.class).in(Scopes.SINGLETON);
            binder.bind(ITicketSystemRepository.class).to(TicketSystemRepository.class).in(Scopes.SINGLETON);
            
            binder.bind(ARepositoryFactory.class).to(RepositoryFactoryImpl.class).in(Scopes.SINGLETON);
            
            binder.requestStaticInjection(ARepositoryFactory.class);
        }
        
    }
}
